package com.cloud.system.api.mapper;

import com.cloud.system.api.bean.RecCheckUnitCriteriaBean;
import com.cloud.system.api.bean.RecPunishSuperviseItemCriteriaBean;
import com.cloud.system.api.bean.SuperviseBean;
import com.cloud.system.api.entity.UnitSupervise;
import com.cloud.system.api.provider.SuperviseBeanPageProvider;
import com.cloud.system.common.driver.SimpleInlangDriver;
import com.cloud.system.common.utils.BaseMapperHelper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface UnitSuperviseMapper extends BaseMapperHelper<UnitSupervise> {

    @Select("SELECT * FROM unit_supervise WHERE  sup_code in  (SELECT sup_code FROM  unit_supervise_claim WHERE social_credit_code = #{supCode}) UNION SELECT * FROM unit_supervise WHERE sup_unit_id = #{unitId}")
    @Lang(SimpleInlangDriver.class)
    @Results({
            @Result(property = "supId",column = "sup_id",javaType = Integer.class),
            @Result(property = "supUnitName", column = "sup_unit_name", javaType = String.class),
            @Result(property = "supName", column = "sup_name", javaType = String.class),
            @Result(property = "supCode", column = "sup_code", javaType = String.class),
            @Result(property = "permitName", column = "permit_name", javaType = String.class),
            @Result(property = "permitTypeName", column = "permit_type_name", javaType = String.class),
            @Result(property = "permitCode", column = "permit_code", javaType = String.class),
            @Result(property = "supSubItem", column = "sup_sub_item", javaType = String.class),
            @Result(property = "isPromiseApproval", column = "is_promise_approval", javaType = String.class),
            @Result(property = "supTargets", column = "sup_targets", javaType = String.class),
            @Result(property = "supMode", column = "sup_mode", javaType = String.class),
            @Result(property = "supMethod", column = "sup_method", javaType = String.class),
            @Result(property = "lawItemContent", column = "law_item_content", javaType = String.class),
            @Result(property = "workFlow", column = "work_flow", javaType = String.class),
            @Result(property = "supResult", column = "sup_result", javaType = String.class),
            @Result(property = "supLevelName", column = "sup_level_name", javaType = String.class),
            @Result(property = "stateName", column = "state_name", javaType = String.class),
            @Result(property = "version", column = "version", javaType = Integer.class),
            @Result(property = "planEffectiveDate", column = "plan_effective_date", javaType = Date.class),
            @Result(property = "planExpiredDate", column = "plan_expired_date", javaType = Date.class),
    })
    List<UnitSupervise> listPage(@Param("supCode")String supCode,@Param("unitId")Long unitId);

    @SelectProvider(type = SuperviseBeanPageProvider.class, method = "searchCheckItem")
    @Results({
            @Result(property = "supItemCode",column = "sup_item_code",javaType = String.class),
            @Result(property = "supCode", column = "sup_code", javaType = String.class),
            @Result(property = "supSubItem", column = "sup_sub_item", javaType = String.class),
            @Result(property = "supId", column = "sup_id", javaType = Integer.class),
            @Result(property = "inspectExecuteListCode", column = "inspect_execute_list_code", javaType = String.class),
    })
    List<SuperviseBean> searchCheckItem(RecCheckUnitCriteriaBean recCheckUnitCriteriaBean);

    @SelectProvider(type = SuperviseBeanPageProvider.class, method = "searchSuperviseItem")
    @Results({
            @Result(property = "supCode", column = "sup_code", javaType = String.class),
            @Result(property = "supName", column = "sup_name", javaType = String.class),
            @Result(property = "supSubItem", column = "sup_sub_item", javaType = String.class),
            @Result(property = "supId", column = "sup_id", javaType = Integer.class),
    })
    List<SuperviseBean> searchSuperviseItem(RecPunishSuperviseItemCriteriaBean recPunishSuperviseItemCriteriaBean);

    @Select("SELECT sup_code FROM unit_supervise WHERE social_credit_code = #{socialCreditCode} UNION SELECT t2.sup_code FROM unit_supervise t1, unit_supervise_claim t2 WHERE t2.social_credit_code = #{socialCreditCode}  AND t2.sup_code = t1.sup_code GROUP BY t2.sup_code")
    List<String> getSupCodeBySocialCreditCode(String socialCreditCode);
}